const app = getApp();

Page({
  data: {
    allList: [],
    allCate: [],
    catId : '',
    countPage:'',
    page:1
  },
  onLoad(r) {
    my.setNavigationBar({
      title: '全部商品',
    });
    var that = this;
    var cid = r.id;
    if(cid){
      that.setData({
        catId: cid,
        id:cid
      });
    }
    that.goodsList();
    my.httpRequest({
      url: app.geturl+'/goods-class/list',
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        if(res.data.code==0){
          that.setData({
            allCate: res.data.data.list
          });
        }
      }
    });
  },
  onPullDownRefresh() {
    var that = this;
    that.setData({
      page:1
    });
    that.goodsList();
    my.stopPullDownRefresh();
  },
  onReachBottom() {
    var that = this;
    if(that.data.countPage>that.data.page){
      that.setData({
        page:that.data.page+1
      });
      that.goodsList();
    }
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  goodsList(){
    var that = this;
    var id = that.data.catId;
    my.httpRequest({
      url: app.geturl+'/goods-common/list',
      method: 'GET',
      data: {
        goods_class_id: id,
        page:that.data.page
      },
      dataType: 'json',
      success: function(res) {
        if(res.data.code==0){
          that.setData({
            allList: res.data.data.list,
            countPage:res.data.data.pageArr.pageTotal
          })
        }
      }
    });
  },
  allGoods(){
    var that = this;
    that.setData({
      catId: ''
    });
    that.goodsList();
  },
  cateSearch(e){
    var that = this;
    that.setData({
      catId: e.target.dataset.id
    });
    that.goodsList();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  },
  goodsDetail(e){
    var id = e.target.dataset.id;
    my.navigateTo({ url: '../add-todo/detail?id='+id });
  },
  myList(){
    my.navigateTo({ url: '../add-todo/member' });
  },
});
